﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using MvcLibrary.Repository;
using System.IO;
using WebVideo.Areas.Admin.Models;
using MvcLibrary.Repository;

namespace WebVideo.Areas.Admin.Controllers
{
    public class VideoController : Controller
    {
        //
        // GET: /Admin/Video/

        public ActionResult Index()
        {
            dbVideoDataContext db = new dbVideoDataContext();
            // delete video quá 30 ngày không gia hạn
            var q = from p in db.EntityVideos
                    where p.Viewed_Date != null
                    select p;
            db.EntityVideos.DeleteAllOnSubmit(q.Where(g => (DateTime.Now - g.Viewed_Date.Value).TotalDays >= 30).ToList());
            db.SubmitChanges();
            //danh sách doanh nghiệp
            ViewBag.DSDoanhNghiep = db.EntityBusinesses.ToList();

            return View(VideoModel.LayDuLieu ());
        }
        #region "Insert"
        public ActionResult Insert()
        {
            VideoModel tnew = new VideoModel();
            return View(tnew);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(VideoModel Mtnew, HttpPostedFileBase Image, string City)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Ảnh phải nhỏ hơn 2MB." });
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Không thể upload ảnh" });

                        var path = Path.Combine(Server.MapPath("~/Content/Images/Video"), encodestring);
                        Image.SaveAs(path);

                        Mtnew.Image = encodestring;
                    }
                    if (Mtnew.Criteria == "City")
                        Mtnew.Criteria = City;
                    VideoModel.Insert(Mtnew);
                    return RedirectToAction("Index", "Video");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(Mtnew);
        }
        #endregion //insert


        #region "Edit"
        public ActionResult Edit(int IdVideo)
        {


            return View(VideoModel.LayModel(IdVideo,0));

        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(VideoModel Mtnew, HttpPostedFileBase Image, string City)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Ảnh phải nhỏ hơn 2MB." });
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Không thể upload ảnh" });

                        var path = Path.Combine(Server.MapPath("~/Content/Images/Video"), encodestring);
                        Image.SaveAs(path);

                        Mtnew.Image = encodestring;
                    }
                    if(Mtnew.Criteria == "City")
                         Mtnew.Criteria = City;
                   string image =  VideoModel.Edit(Mtnew);
                   if (image != null)
                   {
                       string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/Video"), image); // file hinh cu
                       System.IO.File.Delete(fileToDelete);
                   }
                    return RedirectToAction("Index", "Video");
                }
            }
            catch
            {

                return RedirectToAction("Index", "Error", new { errorMsg = "Lỗi SQL hosting / server" });
            }
            return View(Mtnew);
        }
        #endregion //Edit


        #region "Delete"
        public string Delete(int Id)
        {
            try
            {
               string image = VideoModel.Delete(Id);
               if (image != null)
               {
                   string fileToDelete = Path.Combine(Server.MapPath("~/Content/Images/Video"), image); // file hinh cu
                   System.IO.File.Delete(fileToDelete);
               }
                return "ok";

            }
            catch
            {
                return "error";

            }

        }

        #endregion //Delete

        #region "Hide"
        public string Hide(int IdVideo, int l)
        {
            try
            {

                return VideoModel.Hide(IdVideo, l);
            }
            catch
            {
                return "error";

            }

        }

        #endregion //Hide

        #region "View"
        public ActionResult View(int IdVideo)
        {


            return View(VideoModel.LayModel(IdVideo,1));

        }
        #endregion // view
    }


}
